package com.yxhl.zoume.service;

import android.app.IntentService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.yxhl.zoume.ZouMeApplication;
import com.yxhl.zoume.data.tcp.TcpClient;
import com.yxhl.zoume.di.component.tcp.DaggerTcpComponent;

import javax.inject.Inject;

/**
 * Author：zhongshan   
 * Date：2016/12/14 10:39      
 * Description：
 */
public class TcpControlService extends IntentService {

    @Inject
    TcpClient client;

    private static final String SERVICE_NAME = "TCP_CONTROL_SERVICE";
    private NetworkChangeReceiver mNetworkChangeReceiver;

    public TcpControlService() {
        super(SERVICE_NAME);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initializeInjector();
        registerNetWorkReceiver();
        onRxBusEvent();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        startTcpConnection();
    }

    private void startTcpConnection() {
        TcpClient.isHandClose = false;
        TcpClient.isConnect = false;
        client.connect();
    }


    @Override
    public void onDestroy() {
        unregisterNetWorkReceiver();
        client.close();
        super.onDestroy();
    }

    private void initializeInjector() {
        DaggerTcpComponent.builder()
                .appComponent(ZouMeApplication.application.getAppComponent())
                .build()
                .inject(this);
    }

    private void registerNetWorkReceiver() {
        if (mNetworkChangeReceiver == null) {
            mNetworkChangeReceiver = new NetworkChangeReceiver();
            registerReceiver(mNetworkChangeReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        }
    }

    private void unregisterNetWorkReceiver() {
        if (mNetworkChangeReceiver != null) {
            unregisterReceiver(mNetworkChangeReceiver);
        }
    }

    private void onRxBusEvent() {

    }

    //    网络监听，断网到重连网络（程序已进入，就会调用)
    protected class NetworkChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //从无网络到有网络连接时运行该广播回调方法
            if (!client.isConnect()) {
                String action = intent.getAction();
                if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
                    if (networkInfo != null && networkInfo.isAvailable()) {
                        int type = networkInfo.getType();
                        switch (type) {
                            case ConnectivityManager.TYPE_WIFI:
                                break;
                            case ConnectivityManager.TYPE_MOBILE:
                                break;
                        }
                    }
                    //没有网络链接
                    else {
                    }
                }
            }
        }
    }

}
